package dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Lesson {
    public int lid;
    public String lessonName;
    public int sectionId;
    public String videoPath;

    private static Connection connection=DBConnection.getDBCourseConnection();

    public static List<Lesson> getLessonsByCid(int cid){
        String sql="select lid,lessonName,lesson.sectionId,videoPath from lesson,section where lesson.sectionId=section.sectionId and section.cid=?";
        PreparedStatement preparedStatement= null;
        List<Lesson> lessons = new ArrayList<>();
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,cid);
            ResultSet resultSet=preparedStatement.executeQuery();
            while (resultSet.next()){
                Lesson lesson=new Lesson();
                lesson.lid=resultSet.getInt(1);
                lesson.lessonName=resultSet.getString(2);
                lesson.sectionId=resultSet.getInt(3);
                lesson.videoPath=resultSet.getString(4);
                lessons.add(lesson);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return lessons;
    }

    public static int insertInfo(String lessonName,int sectionId){
        int result=0;
        try {
            CallableStatement addCourse=connection.prepareCall("{call add_lesson(?,?,?)}");
            addCourse.setString(1,lessonName);
            addCourse.setInt(2,sectionId);
            addCourse.registerOutParameter(3, Types.INTEGER);
            addCourse.execute();
            result=addCourse.getInt(3);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static List<Lesson> getLessonsBySectionId(int sectionId){
        String sql="select lid,lessonName,videoPath from lesson where sectionId=?";
        PreparedStatement preparedStatement= null;
        List<Lesson> lessons = new ArrayList<>();
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,sectionId);
            ResultSet resultSet=preparedStatement.executeQuery();
            while (resultSet.next()){
                Lesson lesson=new Lesson();
                lesson.lid=resultSet.getInt(1);
                lesson.lessonName=resultSet.getString(2);
                lesson.videoPath=resultSet.getString(3);
                lessons.add(lesson);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return lessons;
    }

//    public static int insertInfo(String lessonName,int sectionId,)

}
